package backtrackingalgorithm;

import org.junit.Test;

import java.beans.PropertyEditorManager;

/**
 * @author DuJiabao
 * @create 2021-01-02 10:52
 */
public class Pattern {
    private boolean matched = false;
    private char[] pattern = "h*o?world".toCharArray();
    private int plen = pattern.length;

    public boolean match(char[] text) {
        int length = text.length;
        rmatch(text, 0, 0);
        return matched;
    }

    private void rmatch(char[] text, int textStart, int patternStart) {
        if (matched) return;
        if (patternStart == plen) {
            if (textStart == text.length) {
                matched = true;
            }
            return;
        }
        if (pattern[patternStart] == '*') {
            for (int k = 0; k <= text.length - textStart; k++) { //要进入结束条件
                rmatch(text, textStart + k, patternStart + 1);
            }
        } else if (pattern[patternStart] == '?') {
            rmatch(text, textStart + 1, patternStart + 1);//‘？’代替一个字符
            rmatch(text, textStart, patternStart + 1); //'?'为空白
        } else if (textStart < text.length && text[textStart] == pattern[patternStart]) {
            rmatch(text, textStart + 1, patternStart + 1);
        }
    }

    @Test
    public void test() {
        match("hello world".toCharArray());
        System.out.println(matched);
    }

}
